package com.travelbid.presentation.service;

import java.util.List;

import org.hibernate.Query;

import com.travelbid.presentation.dao.TPresentationDAO;
import com.travelbid.presentation.pojo.TDetail;
import com.travelbid.presentation.pojo.TDetailDAO;
import com.travelbid.presentation.pojo.TPresentation;
import com.travelbid.presentation.struts.form.AddDetailForm;
import com.travelbid.presentation.struts.form.AddPresentationForm;

public class PresentationService {

	private TPresentationDAO presentationDAO;
	private TDetailDAO detailDAO;
	
	public void addNewPresentation(AddPresentationForm addPresentationForm,Integer parId){
		TPresentation presentation = new TPresentation();
		presentation.setParentId(parId);
		presentation.setPresentationName(addPresentationForm.getPresentationName());
		presentationDAO.save(presentation);
	}
	
	@SuppressWarnings("unchecked")
	public List<TPresentation> getPresentationListByParId(Integer parId){		
		return presentationDAO.findByParentId(parId);
	}
	
	public TPresentation getPresentationById(Integer id){
		return presentationDAO.findById(id);
	}
	
	public TDetail getDetailById(Integer id){
		return detailDAO.findById(id);
	}

	public void addNewDetail(AddDetailForm addDetailForm,Integer parId){
		TDetail detail = new TDetail();
		detail.setParentId(parId);
		detail.setDetailName(addDetailForm.getDetailName());
		detail.setContent(addDetailForm.getContent());
		detailDAO.save(detail);
	}
	
	public void editDetail(AddDetailForm addDetailForm,Integer id){
		TDetail detail = detailDAO.findById(id);
		detail.setParentId(addDetailForm.getParId());
		detail.setDetailName(addDetailForm.getDetailName());
		detail.setContent(addDetailForm.getContent());
		detailDAO.save(detail);
	}
	
	@SuppressWarnings("unchecked")
	public List<TPresentation> findAllParent(){
		String hql = "from TPresentation cate where cate.parentId in(select c.id from TPresentation c where c.parentId = 0)";
		List<TPresentation> list = detailDAO.getHibernateTemplate().find(hql);
		return list;
	}
	
	@SuppressWarnings("unchecked")
	public List<TDetail> findAllDetail(String parId,Integer pageNum){
		String hql = "";
		if(parId!=null&&!parId.equals("-1")){
			hql = "from TDetail cate where cate.parentId ="+parId;
		}else{
			hql = "from TDetail";
		}
		Query query = detailDAO.getSessionFactory().openSession().createQuery(hql);
		query.setFirstResult((pageNum-1)*20);
		query.setMaxResults(20);
		List<TDetail> list = query.list();
		return list;
	}	
	
	@SuppressWarnings("unchecked")
	public Integer findAllDetailCount(String parId){
		String hql = "";
		if(parId!=null&&!parId.equals("-1")){
			hql = "from TDetail cate where cate.parentId ="+parId;
		}else{
			hql = "from TDetail";
		}
		List<TDetail> list = detailDAO.getHibernateTemplate().find(hql);
		return list.size();
	}

	public void deletePresentation(String[] delId){
		for(String id : delId){
			TPresentation presentation = presentationDAO.findById(new Integer(id));
			if(presentation.getParentId().equals(0)){
				for(TPresentation p : (List<TPresentation>)presentationDAO.findByParentId(presentation.getPresentationId())){
					deleteSubPresentation(p);
				}
			}else{
				deleteSubPresentation(presentation);
			}
			presentationDAO.delete(presentation);
		}
	}
	
	public void deleteSubPresentation(Integer id){
		TPresentation presentation = presentationDAO.findById(id);
		for(TDetail detail : (List<TDetail>)detailDAO.findByParentId(presentation.getPresentationId())){				
			detailDAO.delete(detail);
		}
		presentationDAO.delete(presentation);
	}
	
	public void deleteSubPresentation(TPresentation presentation){
		for(TDetail detail : (List<TDetail>)detailDAO.findByParentId(presentation.getPresentationId())){				
			detailDAO.delete(detail);
		}
		presentationDAO.delete(presentation);
	}
	
	public void deleteDetail(String[] delId){
		for(String id : delId){
			detailDAO.delete(detailDAO.findById(new Integer(id)));
		}
	}
	
	public void editPresentation(TPresentation presentation){
		presentationDAO.save(presentation);
	}

	public TPresentationDAO getPresentationDAO() {
		return presentationDAO;
	}

	public void setPresentationDAO(TPresentationDAO presentationDAO) {
		this.presentationDAO = presentationDAO;
	}

	public TDetailDAO getDetailDAO() {
		return detailDAO;
	}

	public void setDetailDAO(TDetailDAO detailDAO) {
		this.detailDAO = detailDAO;
	}
}
